﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace BobcatUtility
{
    /// <summary>
    /// A class that holds (mostly static) functions for manipulating XML data.
    /// </summary>
    public class XMLUtility
    {
        /// <summary>
        /// Makes an XML element with the name and value passed in.
        /// </summary>
        public static string makeElement(string strElementName, string strValue)
        {
            string strXML = "<" + strElementName + ">" +
                            strValue + 
                            "</" + strElementName + ">";
            return strXML;
        }

        /// <summary>
        /// Makes an XML element with the name and value passed in.
        /// </summary>
        public static string makeElement(string strElementName, int iValue)
        {
            return makeElement(strElementName, iValue.ToString());
        }

        /// <summary>
        /// Makes an XML element with the name and value passed in.
        /// </summary>
        public static string makeElement(string strElementName, bool bValue)
        {
            return makeElement(strElementName, bValue.ToString());
        }

        /// <summary>
        /// Returns the string value of the sub-element passed in of the 'node' passed in.
        /// Returns "" if the element was not found.
        /// </summary>
        public static string getElementStringValue(string strElementName, XmlNode node)
        {
            // We find the element...
            string result = "";
            XmlNode elementNode = node.SelectSingleNode(strElementName);
            if (elementNode != null)
            {
                result = elementNode.InnerXml;
            }
            return result;
        }

        /// <summary>
        /// Returns the bool value of the element passed in.
        /// Returns false if the value was not present.
        /// </summary>
        public static bool getElementBoolValue(string strElementName, XmlNode node)
        {
            bool result = false;
            string strValue = getElementStringValue(strElementName, node);
            if (strValue != "")
            {
                result = Convert.ToBoolean(strValue);
            }
            return result;
        }

        /// <summary>
        /// Returns the int value of the element passed in.
        /// Returns 0 if the value was not present.
        /// </summary>
        public static int getElementIntValue(string strElementName, XmlNode node)
        {
            int result = 0;
            string strValue = getElementStringValue(strElementName, node);
            if (strValue != "")
            {
                result = Convert.ToInt32(strValue);
            }
            return result;
        }
    }
}
